package com.bootdo.wx.service.impl;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;
import com.bootdo.common.utils.UUIDUtils;
import com.bootdo.wx.dao.MemberDoctorDao;
import com.bootdo.wx.dao.MemberUserDao;
import com.bootdo.wx.domain.MemberDoctorDO;
import com.bootdo.wx.domain.MemberUserDO;
import com.bootdo.wx.service.MemberDoctorService;



@Service
public class MemberDoctorServiceImpl implements MemberDoctorService {
	@Autowired
	private MemberDoctorDao memberDoctorDao;
	@Autowired
	private MemberUserDao memberUserDao;
	
	@Override
	public MemberDoctorDO get(String id){
		return memberDoctorDao.get(id);
	}
	
	@Override
	public List<MemberDoctorDO> list(Map<String, Object> map){
		return memberDoctorDao.list(map);
	}
	
	@Override
	public int count(Map<String, Object> map){
		return memberDoctorDao.count(map);
	}
	
	@Override
	public int save(MemberDoctorDO memberDoctor){
		return memberDoctorDao.save(memberDoctor);
	}
	
	@Override
	public int update(MemberDoctorDO memberDoctor){
		return memberDoctorDao.update(memberDoctor);
	}
	
	@Override
	public int remove(String id){
		return memberDoctorDao.remove(id);
	}
	
	@Override
	public int batchRemove(String[] ids){
		return memberDoctorDao.batchRemove(ids);
	}

	@Override
	public R shen(MemberDoctorDO memberDoctor) {
		/*if(memberDoctor.getStatus()==1){
			MemberDoctorDO memberDoctorDO = memberDoctorDao.get(memberDoctor.getId());
			MemberUserDO memberUserDO = memberUserDao.get(memberDoctorDO.getUserId());
			//上级用户
			String indirectUser = memberUserDO.getIndirectUser();
			MemberUserDO upMemberUserDO = memberUserDao.get(indirectUser);
			if(upMemberUserDO!=null){
				//如果上级是患者,间接推荐是医生,那么医生间接推荐人就是县,把本用户间接推荐人设置成县
				if(upMemberUserDO.getLevel()==4){
					MemberUserDO memberUserDO2 = memberUserDao.get(upMemberUserDO.getIndirectUser());
					memberUserDO.setIndirectUser(memberUserDO2.getIndirectUser());
				}else if(upMemberUserDO.getLevel()==3){
					//如果上级是医生,那么间接推荐人就是县,把本用户间接推荐人设置成县
					memberUserDO.setIndirectUser(upMemberUserDO.getIndirectUser());
				}
			}
			memberUserDO.setLevel(3);
			memberUserDO.setUpdateAt(new Date());
			memberUserDao.update(memberUserDO);
			
		}*/
		memberDoctorDao.update(memberDoctor);
		return R.ok();
	}

	@Override
	public List<MemberDoctorDO> likeList(Map<String, Object> map) {
		// TODO Auto-generated method stub
		return memberDoctorDao.likeList(map);
	}

	@Override
	public R authDoctor(Map<String, Object> map) {
		MemberDoctorDO findByUserId = memberDoctorDao.findByUserId(map.get("userId").toString());
		if(findByUserId!=null&&findByUserId.getStatus()<2){
			return R.error("请勿重复认证");
		}
		try {
			MemberDoctorDO memberDoctorDO = new MemberDoctorDO();
			BeanUtils.populate(memberDoctorDO, map);
			memberDoctorDO.setId(UUIDUtils.getUUID());
			memberDoctorDO.setDelFlag(0);
			memberDoctorDO.setStatus(0);
			memberDoctorDO.setCaAt(new Date());
			memberDoctorDao.save(memberDoctorDO);
			return R.ok().put("data", memberDoctorDO);
		} catch (Exception e) {
			e.printStackTrace();
			return R.error();
		}
		
		
	}

	@Override
	public R getAuthDoctorInfo(Map<String, Object> map) {
		MemberDoctorDO memberDoctorDO = memberDoctorDao.findByUserId(map.get("userId").toString());
		return R.ok().put("data", memberDoctorDO);
	}

	
}
